package com.google.code.basic.oop;

import java.text.SimpleDateFormat;
import java.util.Date;

public final class LoggerFormatter
{
  public static final String OPEN_BRACKET = "[";
  public static final String CLOSE_BRACKET = "]";
  private static SimpleDateFormat s_dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");



   public static String stackTraceToString(Throwable e) {
		StringBuilder sb = new StringBuilder();
		for (StackTraceElement element : e.getStackTrace()) {
			sb.append(element.toString());
				sb.append("\n");
	  }
	  return sb.toString();
	}



  public static String format(int _depth, Level _level, String _format)
  {
    StringBuilder message = new StringBuilder();

    message.append("[").append(_level).append("]");

    caller(message, _depth + 1);
    message.append("[").append(_format).append("]");

    return message.toString();
  }

  public static void currentTime(StringBuilder _buffer)
  {
    long curr = System.currentTimeMillis();
    _buffer.append("[").append(s_dateFormatter.format(new Date(curr))).append("]");
  }

  public static void caller(StringBuilder _buffer, int _depth)
  {
    if (_buffer != null) {
      StackTraceElement[] elements = Thread.currentThread().getStackTrace();

      StackTraceElement evokedBy = elements[(_depth + 1)];

      _buffer.append("[").append(evokedBy.getClassName()).append(".").append(evokedBy.getMethodName()).append("():").append(evokedBy.getLineNumber()).append("]");
    }
  }



  public static enum Level
  {
    DEBUG, INFO, WARN, ERROR, FATAL;
  }
}